package logic

import (
	"context"

	"go-zero-mall/app/admin/cmd/rpc/internal/svc"
	"go-zero-mall/app/admin/cmd/rpc/pd"
	"go-zero-mall/app/admin/model"

	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
)

type RoleAllocMenuLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRoleAllocMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RoleAllocMenuLogic {
	return &RoleAllocMenuLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 角色-分配菜单
func (l *RoleAllocMenuLogic) RoleAllocMenu(in *pd.UmsRoleAllocMenuReq) (*pd.Response, error) {
	roleMenu, err := l.svcCtx.UmsRoleMenuRelationModel.FindOne(l.ctx, in.RoleId)
	if err != nil {
		return nil, err
	}
	var d model.UmsRoleMenuRelation
	copier.Copy(&d, in)
	if roleMenu == nil {
		l.svcCtx.UmsRoleMenuRelationModel.Insert(l.ctx, &d)
	} else {
		l.svcCtx.UmsRoleMenuRelationModel.Update(l.ctx, &d)
	}
	return &pd.Response{}, nil
}
